<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
    <el-col v-if="isRoot">
      <strong> <i class="el-icon-loading" />{{ nodeData.name }}</strong>
    </el-col>
    <el-col v-else>
      <span> {{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown>
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addeparment">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="editDepartment">编辑子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delpartment(nodeData.id)">删除子部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delpartment } from '@/api/department'
export default {
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  // 函数
  methods: {
  // 点击删除部门
    async  delpartment(id) {
      try {
        // 1.提示用户
        await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', { confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning' })
        // 2.删除数据
        await delpartment(id)
        // 3.提醒用户
        this.$message.success('删除成功')
        // 4.更新页面
        this.$emit('delpartment')
      } catch (error) {
        console.log(error)
      }
    },

    // 子组件告诉父组件点击了添加部门
    addeparment() {
      this.$emit('addeparment', this.nodeData)
    },
    // 编辑部门
    editDepartment() {
      this.$emit('editDepartment', this.nodeData)
    }
  }
}
</script>

<style>

</style>
